﻿using System;
using System.Collections.Generic;
using System.Linq;
using Acme.Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Acme.Business.Tests
{

    /// <summary>
    ///This is a test class for EventRepositoryTest and is intended
    ///to contain all EventRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class EventRepositoryFixture
    {

        TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        [TestMethod()]
        public void EventRepositoryConstructorTest()
        {
            // test verifies that the EventRepository can be created.
            // if not, an exception will be throw.
            var target = new EventRepository();
        }

        [TestMethod()]
        public void CreateTest()
        {

            //arrange
            var target = new EventRepository();
            Event actual;

            //act
            actual = target.Create();

            //assert
            Assert.IsNotNull(actual);
            Assert.AreNotEqual(actual.Id, Guid.Empty);
        }

        [TestMethod()]
        public void GetByIdTest()
        {

            //arrange
            var target = new EventRepository();
            EventSearchResult searchResult = target.GetSearchResults().AsQueryable().First();
            Event actual;

            //act
            actual = target.GetById(searchResult.Id);

            //assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(searchResult.Id, actual.Id);
        }

        [TestMethod()]
        public void GetCountTest()
        {

            //arrange
            var target = new EventRepository();
            const Int32 expected = 2;
            Int32 actual;

            //act
            actual = target.GetCount();

            //assert
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void GetSearchResultsTest()
        {

            //arrange
            var target = new EventRepository();
            IEnumerable<EventSearchResult> actual;
            const Int32 expected = 2;

            //act
            actual = target.GetSearchResults();

            //assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(actual.Count(), expected);
        }

        [TestMethod()]
        public void RemoveTest()
        {

            //arrange
            var target = new EventRepository();
            EventSearchResult searchResult = target.GetSearchResults().AsQueryable().First();
            Event eventToRemove;
            const Int32 expected = 1;
            Int32 actual;

            //act
            eventToRemove = target.GetById(searchResult.Id);
            actual = target.Remove(eventToRemove);

            //assert
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void SaveTest()
        {

            //arrange
            var target = new EventRepository();
            var @event = new Event() { Title = "Christmas 2009", Location = "Home" };
            Event retrieveAfterSaving;
            const Int32 expected = 1;
            Int32 actual;

            //act
            actual = target.Save(@event);
            retrieveAfterSaving = target.GetById(@event.Id);

            //assert
            Assert.AreNotEqual(@event, retrieveAfterSaving);  //verifies GetById returns a DeepCopy
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(@event.Title, retrieveAfterSaving.Title);
        }
    }
}
